package com.hr.demo.service.impl;

import java.io.File;
import java.io.IOException;

import com.hr.demo.config.State;
import com.hr.demo.Properties.UploadProperties1;
import com.hr.demo.entity.ResponseJson;
import com.hr.demo.entity.User;
import com.hr.demo.mapper.FileMapper;
import com.hr.demo.service.FileService;
import com.hr.demo.utils.TimeUtil;
import com.hr.demo.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private ThreadLocal<User> threadLocal;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private UploadProperties1 uploadProperties1;

    @Override
    public ResponseJson getMyFile() {
        User user = threadLocal.get();
        com.hr.demo.entity.File file = fileMapper.getFileById(user.getId());
        return new ResponseJson(State.SUCCESS, "查询成功", file);
    }

    @Override
    public ResponseJson seyMyFile(MultipartFile file) {
        com.hr.demo.entity.File file1 = new com.hr.demo.entity.File();
        User user = threadLocal.get();
        file1.setUserId(user.getId());
//        if (!uploadProperties1.getAllowTypes().contains(file.getContentType())) {
//            return new ResponseJson(State.FAIL, "文件上传类型错误");
//        }
        file1.setFileName(file.getOriginalFilename());
        file1.setFileLength(file.getSize());
        if (file.getSize() % 1024 > 0) {
            if (file.getSize() % 1024 % 1024 > 0) {
                if (file.getSize() % 1024 % 1024 % 1024 > 0) {
                    if (file.getSize() % 1024 % 1024 % 1024 % 1024 > 0) {
                        if (file.getSize() % 1024 % 1024 % 1024 % 1024 % 1024 > 0) {
                            if (file.getSize() % 1024 % 1024 % 1024 % 1024 % 1024 % 1024 > 0) {
                                if (file.getSize() % 1024 % 1024 % 1024 % 1024 % 1024 % 1024 % 1024 > 0) {

                                } else {
                                    file1.setFileSize(String.valueOf(file.getSize() / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 + "EB"));
                                }
                            } else {
                                file1.setFileSize(String.valueOf(file.getSize() / 1024 / 1024 / 1024 / 1024 / 1024 + "PB"));
                            }
                        } else {
                            file1.setFileSize(String.valueOf(file.getSize() / 1024 / 1024 / 1024 / 1024 + "TB"));
                        }
                    } else {
                        file1.setFileSize(String.valueOf(file.getSize() / 1024 / 1024 / 1024 + "GB"));
                    }
                } else {
                    file1.setFileSize(String.valueOf(file.getSize() / 1024 / 1024 + "MB"));
                }
            } else {
                file1.setFileSize(String.valueOf(file.getSize() / 1024 + "KB"));
            }
        } else {
            file1.setFileSize(String.valueOf(file.getSize()) + "B");
        }
        String fileName = UploadUtil.generateFileName(file.getOriginalFilename());
        file1.setFilePath(uploadProperties1.getPath());
        try {
            file.transferTo(new File(uploadProperties1.getPath() + fileName));
            file1.setFileTime(TimeUtil.getCurrentTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = fileMapper.insertFile(file1);
        if (i==1){
            return new ResponseJson(State.SUCCESS,"文件上传成功",file1);
        }
        return new ResponseJson(State.FAIL,"文件上传失败");
    }
}
